﻿using z_tool.config;
using Newtonsoft.Json;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using z_tool.config;
using z_tool.rtt_java_1._0;
using z_tool.rtt_vue_sys_1._0;

namespace z_tool
{
    public partial class Form1 : Form
    {
        bool form_loading = true;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            log("我是记日志的……");

            baseconfig.db_reset = db_reset;
            baseconfig.log = log;
            baseconfig.listpath = listpath;
            baseconfig.save_temporary = save_temporary;
            //读取上次保存
            string filename = string.Format("{0}template\\template.json", System.AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(filename))
            {
                string json = System.IO.File.ReadAllText(filename);
                baseconfig.initial = true;
                basesave _basesave = JsonConvert.DeserializeObject<basesave>(json);
                baseconfig.root_path = _basesave.root_path;
                textBox1.Text = _basesave.root_path;
                baseconfig.db_conn_str = _basesave.db_conn_str;
                textBox2.Text = _basesave.db_conn_str;
                baseconfig.tables = _basesave.tables;
                baseconfig.generate_type = _basesave.generate_type;
            }
            string cdir = System.Environment.CurrentDirectory;
            string template = string.Format("{0}\\template", cdir);
            if (!Directory.Exists(template))
            {
                Directory.CreateDirectory(template);
            }
            saveFileDialog1.InitialDirectory = template;
            openFileDialog1.InitialDirectory = template;
            baseconfig.initial = false;
            if (!string.IsNullOrEmpty(textBox2.Text)) db_reset();
            comboBox1.DataSource = baseconfig.generate_type;
            comboBox1.DisplayMember = "name";
            comboBox1.SelectedIndex = baseconfig.generate_type.IndexOf(baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            string path = folderBrowserDialog1.SelectedPath;
            if (!string.IsNullOrEmpty(path))
            {
                textBox1.Text = path;
                baseconfig.root_path = path;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            baseconfig.db_conn_str = textBox2.Text;
        }

        private void loading()
        {
            timer1.Enabled = true;
            toolStripStatusLabel1.Text = "狗鸡巴在工作……";
        }

        private void clearloading()
        {
            timer1.Enabled = false;
            statusStrip1.BeginInvoke(new Action(() =>
            {
                toolStripStatusLabel1.Text = "狗鸡巴没干活";
                toolStripProgressBar1.Value = new Random().Next(0, 100);
            }));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            statusStrip1.BeginInvoke(new Action(() =>
            {
                toolStripProgressBar1.PerformStep();
                if (toolStripProgressBar1.Value >= toolStripProgressBar1.Maximum)
                    toolStripProgressBar1.Step = -10;
                if (toolStripProgressBar1.Value <= 0)
                    toolStripProgressBar1.Step = 10;
            }));
        }

        private void log(string log)
        {
            textBox3.BeginInvoke(new Action(() =>
            {
                textBox3.AppendText(string.Format("{0}:{1}", DateTime.Now.ToString(), log));
                textBox3.AppendText(Environment.NewLine);
                textBox3.ScrollToCaret();
            }));
        }

        private void listpath()
        {
            textBox6.BeginInvoke(new Action(() =>
            {
                textBox6.ResetText();
                textBox6.AppendText(">>将生成以下文件<<");
                textBox6.AppendText(Environment.NewLine);
                baseconfig.folder_Items.ForEach(s =>
                {
                    textBox6.AppendText(s.filepath);
                    textBox6.AppendText(Environment.NewLine);
                });
            }));
        }

        private void save_temporary()
        {
            try
            {
                string filename = string.Format("{0}template\\template.json", System.AppDomain.CurrentDomain.BaseDirectory);
                basesave _basesave = new basesave();
                _basesave.root_path = baseconfig.root_path;
                _basesave.db_conn_str = baseconfig.db_conn_str;
                _basesave.tables = baseconfig.tables;
                _basesave.generate_type = baseconfig.generate_type;
                System.IO.File.WriteAllText(filename, JsonConvert.SerializeObject(_basesave));
            }
            catch (Exception ex)
            {
                log(ex.Message);
            }
        }

        private void db_reset()
        {

            //检查步骤1是否完成
            if (baseconfig.root_path == "" || !Directory.Exists(baseconfig.root_path))
            {
                log("步骤1目录未选择或者路径不存在");
                return;
            }
            loading();
            log("数据库连接中……");
            ThreadPool.QueueUserWorkItem(t =>
            {
                string outmsg = "";
                DBHelper dBHelper = new DBHelper(baseconfig.db_conn_str);
                if (dBHelper.checkopen(out outmsg))
                {
                    log("数据库连接成功");
                }
                else log(outmsg);
                clearloading();
            });
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (!baseconfig.initial)
            {
                baseconfig.db_conn_str = "";
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(baseconfig.db_conn_str))
            {
                log("步骤2未设置数据库连接");
                return;
            }
            table_select _Select = new table_select();
            _Select.ShowDialog();
            baseconfig.change(3);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int cknum = baseconfig.tables.Where(s => s.selected == true).Count();
            if (cknum == 0)
            {
                log("步骤3未选择表");
                return;
            }
            col_set _Set = new col_set();
            _Set.ShowDialog();
            baseconfig.change(4);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (form_loading == false)
            {
                foreach (var item in baseconfig.generate_type)
                {
                    var sitem = comboBox1.SelectedItem as itemx;
                    if (item.value == sitem.value) item.selected = true;
                    else item.selected = false;
                }
                baseconfig.change(5);
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            form_loading = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var sitem = baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault();
            if (sitem.value == "1")
            {
                setting_java _Java = new setting_java();
                _Java.ShowDialog();
                baseconfig.change(6);
            }
            else if (sitem.value == "2")
            {
                setting_vue_sys _Java = new setting_vue_sys();
                _Java.ShowDialog();
                baseconfig.change(6);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (baseconfig.tables.Where(s => s.selected == true).Count() == 0)
            {
                log("步骤3未选择表");
                return;
            }
            button5.Text = "生成中……";
            button5.Enabled = false;
            loading();
            ThreadPool.QueueUserWorkItem(t =>
            { 
                var _itemx = baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault();
                current.cur_itemx = _itemx;
                if (_itemx.value == "1")
                {
                    string _package = _itemx.setting["package"].ToString().Replace(".", "/");
                    string _module = _itemx.setting["module"].ToString();
                    #region 生成代码
                    //生成外围代码 
                    //controller_base
                    string _folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}", baseconfig.root_path, _package, _module);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    string _full_path = string.Format("{0}/src/main/java/{1}/controller/{2}Base.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_controller_base _rtt01 = new rtt_java_controller_base();
                    string _str = _rtt01.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("controller/{0}base.java 生成成功", utils.toUpperFirst(_module)));
                    //controller_basecontroller
                    _folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}", baseconfig.root_path, _package, _module);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/controller/{2}BaseController.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_controller_basecontroller _rtt02 = new rtt_java_controller_basecontroller();
                    _str = _rtt02.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("controller/{0}BaseController.java 生成成功", utils.toUpperFirst(_module)));
                    //Application.java
                    _folder_path = string.Format("{0}/src/main/java/{1}", baseconfig.root_path, _package);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/Application.java", baseconfig.root_path, _package);
                    rtt_java_application _rtt03 = new rtt_java_application();
                    _str = _rtt03.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("Application.java 生成成功", _module));
                    //Swagger2Init.java
                    _folder_path = string.Format("{0}/src/main/java/{1}", baseconfig.root_path, _package);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/Swagger2Init.java", baseconfig.root_path, _package);
                    rtt_java_Swagger2Init _rtt04 = new rtt_java_Swagger2Init();
                    _str = _rtt04.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("Swagger2Init.java 生成成功", _module));
                    //pom.xml
                    _folder_path = string.Format("{0}", baseconfig.root_path);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/pom.xml", baseconfig.root_path);
                    rtt_pom _rtt05 = new rtt_pom();
                    _str = _rtt05.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("pom.xml 生成成功", _module));
                    //resources_application.properties
                    _folder_path = string.Format("{0}/src/main/resources", baseconfig.root_path);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/resources/application.properties", baseconfig.root_path);
                    rtt_resources_application _rtt06 = new rtt_resources_application();
                    _str = _rtt06.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("resources/application.properties 生成成功"));
                    //resources_crebas.sql
                    _folder_path = string.Format("{0}/src/main/resources", baseconfig.root_path);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/resources/crebas.sql", baseconfig.root_path);
                    rtt_resources_crebas _rtt07 = new rtt_resources_crebas();
                    _str = _rtt07.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("resources/crebas.sql 生成成功"));
                    //resources_logback-spring.xml
                    _folder_path = string.Format("{0}/src/main/resources", baseconfig.root_path);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/resources/logback-spring.xml", baseconfig.root_path);
                    rtt_resources_logback _rtt08 = new rtt_resources_logback();
                    _str = _rtt08.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("resources/logback-spring.xml 生成成功"));
                    //controller_actuatorController
                    _folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}", baseconfig.root_path, _package, _module);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/controller/ActuatorController.java", baseconfig.root_path, _package);
                    rtt_java_controller_actuator _rtt09 = new rtt_java_controller_actuator();
                    _str = _rtt09.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("controller/ActuatorController.java 生成成功"));
                    //controller_CheckController
                    //_folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}", baseconfig.root_path, _package, _module);
                    //if (!Directory.Exists(_folder_path))
                    //    Directory.CreateDirectory(_folder_path);
                    //_full_path = string.Format("{0}/src/main/java/{1}/controller/{2}/{2}CheckController.java", baseconfig.root_path, _package, _module);
                    //rtt_java_controller_check _rtt010 = new rtt_java_controller_check();
                    //_str = _rtt010.TransformText();
                    //System.IO.File.WriteAllText(_full_path, _str);
                    //log(string.Format("controller/{0}/CheckController.java 生成成功", _module));
                    //tokenFilterSetting.java
                    _folder_path = string.Format("{0}/src/main/java/{1}", baseconfig.root_path, _package);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/TokenFilterSetting{2}.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_tokenFilterSetting _rtt011 = new rtt_java_tokenFilterSetting();
                    _str = _rtt011.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("tokenFilterSetting{0}.java 生成成功", utils.toUpperFirst(_module)));
                    //PubSubConfig.java
                    _folder_path = string.Format("{0}/src/main/java/{1}", baseconfig.root_path, _package);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/PubSubConfig{2}.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_pub_sub_PubSubConfig _rtt012 = new rtt_java_pub_sub_PubSubConfig();
                    _str = _rtt012.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("PubSubConfig{0}.java 生成成功", utils.toUpperFirst(_module)));
                    //pub_sub_Subscriber
                    _folder_path = string.Format("{0}/src/main/java/{1}/pubsub/{2}", baseconfig.root_path, _package, _module);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/pubsub/{2}/Subscriber{2}.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_pub_sub_Subscriber _rtt013 = new rtt_java_pub_sub_Subscriber();
                    _str = _rtt013.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("pubsub/Subscriber{0}.java 生成成功", utils.toUpperFirst(_module)));
                    //pub_sub_SubThread
                    _folder_path = string.Format("{0}/src/main/java/{1}/pubsub/{2}", baseconfig.root_path, _package, _module);
                    if (!Directory.Exists(_folder_path))
                        Directory.CreateDirectory(_folder_path);
                    _full_path = string.Format("{0}/src/main/java/{1}/pubsub/{2}/SubThread{2}.java", baseconfig.root_path, _package, utils.toUpperFirst(_module));
                    rtt_java_pub_sub_SubThread _rtt014 = new rtt_java_pub_sub_SubThread();
                    _str = _rtt014.TransformText();
                    System.IO.File.WriteAllText(_full_path, _str);
                    log(string.Format("pubsub/SubThread{0}.java 生成成功", utils.toUpperFirst(_module)));

                    baseconfig.tables.Where(s => s.selected == true).ToList().ForEach(s =>
                    { 
                        if (s.hasmenu==false && s.p_tab_info == null) return;
                        //写入当前表信息
                        current.cur_table_info = s;
                        //生成bean
                        _folder_path = string.Format("{0}/src/main/java/{1}/bean/{2}", baseconfig.root_path, _package, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/main/java/{1}/bean/{2}/{3}.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_beans _rtt1 = new rtt_java_beans();
                        _str = _rtt1.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("bean/{0}/{1}.java 生成成功", _module, s.table_name_hump));
                        _full_path = string.Format("{0}/src/main/java/{1}/bean/{2}/{3}View.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_beans_view _rtt1_1 = new rtt_java_beans_view();
                        _str = _rtt1_1.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("bean/{0}/{1}View.java 生成成功", _module, s.table_name_hump));
                        //生成mybatis mapper
                        _folder_path = string.Format("{0}/src/main/java/{1}/mapper/{2}", baseconfig.root_path, _package, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/main/java/{1}/mapper/{2}/{3}Mapper.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_mybatis_plus_mapper _rtt2 = new rtt_java_mybatis_plus_mapper();
                        _str = _rtt2.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("mapper/{0}/{1}Mapper.java 生成成功", _module, s.table_name));
                        _full_path = string.Format("{0}/src/main/java/{1}/mapper/{2}/{3}MapperView.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_mybatis_plus_mapper_view _rtt2_1 = new rtt_java_mybatis_plus_mapper_view();
                        _str = _rtt2_1.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("mapper/{0}/{1}MapperView.java 生成成功", _module, s.table_name_hump));
                        //生成controller
                        _folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}", baseconfig.root_path, _package, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/main/java/{1}/controller/{2}/{3}Controller.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_controller _rtt3 = new rtt_java_controller();
                        _str = _rtt3.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("controller/{0}/{1}Controller.java 生成成功", _module, s.table_name));
                        //生成service
                        _folder_path = string.Format("{0}/src/main/java/{1}/service/{2}", baseconfig.root_path, _package, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/main/java/{1}/service/{2}/{3}Service.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_service _rtt4 = new rtt_java_service();
                        _str = _rtt4.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("service/{0}/{1}Service.java 生成成功", _module, s.table_name_hump));
                        //生成service实现
                        _folder_path = string.Format("{0}/src/main/java/{1}/service/{2}/impl", baseconfig.root_path, _package, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/main/java/{1}/service/{2}/impl/{3}ServiceImpl.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                        rtt_java_service_impl _rtt41 = new rtt_java_service_impl();
                        _str = _rtt41.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("service/{0}/impl/{1}ServiceImpl.java 生成成功", _module, s.table_name_hump));

                        //生成api接口
                        if (s.has_api)
                        {
                            //生成api_controller
                            _folder_path = string.Format("{0}/src/main/java/{1}/controller/{2}Api", baseconfig.root_path, _package, _module);
                            if (!Directory.Exists(_folder_path))
                                Directory.CreateDirectory(_folder_path);
                            _full_path = string.Format("{0}/src/main/java/{1}/controller/{2}Api/{3}ApiController.java", baseconfig.root_path, _package, _module, s.table_name_hump);
                            rtt_java_controller_api _rtt5 = new rtt_java_controller_api();
                            _str = _rtt5.TransformText();
                            System.IO.File.WriteAllText(_full_path, _str);
                            log(string.Format("controller/{0}Api/{1}Controller.java 生成成功", _module, s.table_name_hump));
                        }

                    });
                    #endregion
                }
                else if (_itemx.value == "2")
                {
                    string _module = _itemx.setting["module"].ToString();
                    #region 生成代码

                    baseconfig.tables.Where(s => s.selected == true).ToList().ForEach(s =>
                    {
                        //写入当前表信息
                        current.cur_table_info = s;
                        //api
                        string _folder_path = string.Format("{0}/src/api/project/{1}", baseconfig.root_path, _module);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        string _full_path = string.Format("{0}/src/api/project/{1}/project_{1}_{2}.js", baseconfig.root_path, _module, s.table_name);
                        rtt_vue_sys_api _rtt1 = new rtt_vue_sys_api();
                        string _str = _rtt1.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);
                        log(string.Format("api/project/{0}/project_{0}_{1}.js 生成成功", _module, s.table_name));
                        /////////////////////////////////////////////////////////////////
                        if (s.hasmenu == false) return;
                        /////////////////////////////////////////////////////////////////
                        //view
                        if (!s.istree)
                        {
                            #region
                            _folder_path = string.Format("{0}/src/views/project/{1}/{2}", baseconfig.root_path, _module, s.table_name);
                            if (!Directory.Exists(_folder_path))
                                Directory.CreateDirectory(_folder_path);
                            _full_path = string.Format("{0}/src/views/project/{1}/{2}/index.vue", baseconfig.root_path, _module, s.table_name);
                            //非树形
                            if (!s.hasedit)
                            {
                                //仅列表
                                rtt_vue_sys_view_index _rtt2 = new rtt_vue_sys_view_index();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);
                            }
                            //if (s.hasedit && !s.editright && !s.editwindow)
                            if (s.hasedit && !s.editwindow)
                                {
                                //当前页编辑
                                rtt_vue_sys_view_index_edit _rtt2 = new rtt_vue_sys_view_index_edit();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);
                            }
                            //if (s.editright)
                            //{
                            //    //当页右边编辑
                            //    rtt_vue_sys_view_index_edit_right _rtt2 = new rtt_vue_sys_view_index_edit_right();
                            //    _str = _rtt2.TransformText();
                            //    System.IO.File.WriteAllText(_full_path, _str);

                            //}
                            if (s.editwindow)
                            {
                                //新窗口编辑
                                rtt_vue_sys_view_index_edit_window _rtt2 = new rtt_vue_sys_view_index_edit_window();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);
                                //编辑页面
                                _full_path = string.Format("{0}/src/views/project/{1}/{2}/edit.vue", baseconfig.root_path, _module, s.table_name);
                                rtt_vue_sys_view_edit _rtt21 = new rtt_vue_sys_view_edit();
                                _str = _rtt21.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);

                            }
                            if (s.hasedit)
                            {
                                foreach(var ctb in baseconfig.tables.Where(w => w.parent_info == s.table_name).ToList())
                                {
                                    current.cur_table_info = ctb;
                                    //生成关联编辑
                                    _folder_path = string.Format("{0}/src/views/project/{1}/{2}", baseconfig.root_path, _module, ctb.table_name);
                                    if (!Directory.Exists(_folder_path))
                                        Directory.CreateDirectory(_folder_path);
                                    _full_path = string.Format("{0}/src/views/project/{1}/{2}/edit.vue", baseconfig.root_path, _module, ctb.table_name);

                                    rtt_vue_sys_view_edit _rtt2 = new rtt_vue_sys_view_edit();
                                    _str = _rtt2.TransformText();
                                    System.IO.File.WriteAllText(_full_path, _str);
                                    log(string.Format("views/project/{0}/{1}/edit.vue 生成成功", _module, ctb.table_name));
                                }
                            }
                            log(string.Format("views/project/{0}/{1}/index.vue 生成成功", _module, s.table_name));
                            #endregion
                        }
                        else
                        {
                            #region
                            _folder_path = string.Format("{0}/src/views/project/{1}/{2}", baseconfig.root_path, _module, s.table_name);
                            if (!Directory.Exists(_folder_path))
                                Directory.CreateDirectory(_folder_path);
                            _full_path = string.Format("{0}/src/views/project/{1}/{2}/index.vue", baseconfig.root_path, _module, s.table_name);
                            if (!s.hasedit)
                            {
                                //仅列表
                                rtt_vue_sys_view_list_tree _rtt2 = new rtt_vue_sys_view_list_tree();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);
                            }
                            //树形
                            if (s.hasedit && !s.editright && !s.editwindow)
                            {
                                //当夜弹窗
                                rtt_vue_sys_view_list_tree_edit _rtt2 = new rtt_vue_sys_view_list_tree_edit();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);
                            }
                            if (s.editright)
                            {
                                //当页右边编辑
                                rtt_vue_sys_view_list_tree_edit_right _rtt2 = new rtt_vue_sys_view_list_tree_edit_right();
                                _str = _rtt2.TransformText();
                                System.IO.File.WriteAllText(_full_path, _str);

                            }
                            if (s.hasedit)
                            {
                                foreach (var ctb in baseconfig.tables.Where(w => w.parent_info == s.table_name).ToList())
                                {
                                    current.cur_table_info = ctb;
                                    //生成关联编辑
                                    _folder_path = string.Format("{0}/src/views/project/{1}/{2}", baseconfig.root_path, _module, ctb.table_name);
                                    if (!Directory.Exists(_folder_path))
                                        Directory.CreateDirectory(_folder_path);
                                    _full_path = string.Format("{0}/src/views/project/{1}/{2}/edit.vue", baseconfig.root_path, _module, ctb.table_name);

                                    rtt_vue_sys_view_edit _rtt2 = new rtt_vue_sys_view_edit();
                                    _str = _rtt2.TransformText();
                                    System.IO.File.WriteAllText(_full_path, _str);
                                    log(string.Format("views/project/{0}/{1}/edit.vue 生成成功", _module, ctb.table_name));
                                }
                            }
                            log(string.Format("views/project/{0}/{1}/index.vue 生成成功", _module, s.table_name));
                            #endregion
                        }
                        current.cur_table_info = s;
                        //info
                        _folder_path = string.Format("{0}/src/views/project/{1}/{2}", baseconfig.root_path, _module, s.table_name);
                        if (!Directory.Exists(_folder_path))
                            Directory.CreateDirectory(_folder_path);
                        _full_path = string.Format("{0}/src/views/project/{1}/{2}/info.vue", baseconfig.root_path, _module, s.table_name);
                        rtt_vue_sys_view_info _rtt4 = new rtt_vue_sys_view_info();
                        _str = _rtt4.TransformText();
                        System.IO.File.WriteAllText(_full_path, _str);

                    });
                    #endregion
                }
                textBox6.BeginInvoke(new Action(() =>
                {
                    clearloading();
                    button5.Enabled = true;
                    button5.Text = "10.生成(&I)";
                }));
            });
        }

        private void 保存模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "template1.json";
            DialogResult dr =  saveFileDialog1.ShowDialog();
            if(dr== DialogResult.OK)
            {
                basesave _basesave = new basesave();
                _basesave.root_path = baseconfig.root_path;
                _basesave.db_conn_str = baseconfig.db_conn_str;
                _basesave.tables = baseconfig.tables;
                _basesave.generate_type = baseconfig.generate_type;
                System.IO.File.WriteAllText(saveFileDialog1.FileName, JsonConvert.SerializeObject(_basesave));
                log("*****************保存成功*****************");
            }
        }

        private void 载入模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if(dr== DialogResult.OK)
            {
                //读取上次保存
                string filename = openFileDialog1.FileName;
                if (File.Exists(filename))
                {
                    string json = System.IO.File.ReadAllText(filename);
                    baseconfig.initial = true;
                    basesave _basesave = JsonConvert.DeserializeObject<basesave>(json);
                    baseconfig.root_path = _basesave.root_path;
                    textBox1.Text = _basesave.root_path;
                    baseconfig.db_conn_str = _basesave.db_conn_str;
                    textBox2.Text = _basesave.db_conn_str;
                    baseconfig.tables = _basesave.tables;
                    baseconfig.generate_type = _basesave.generate_type;
                }
                else return;
                baseconfig.initial = false;
                if (!string.IsNullOrEmpty(textBox2.Text)) db_reset();
                comboBox1.SelectedIndex = baseconfig.generate_type.IndexOf(baseconfig.generate_type.Where(s => s.selected == true).FirstOrDefault());
                log("*****************载入成功*****************");
            }
        }
    }
}
